package com.br.aiops.alert.message;

import com.br.aiops.counter.AlertLogCounter;
import com.br.aiops.rule.Rule;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Formatter;

@Data
public class LogAlertMessage {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogAlertMessage.class);
    private String message;

    /**
     * 告警负责人
     */
    private String owner;

    /**
     * 匹配的规则
     */
    private Rule rule;

    private LogAlertMessage(AlertLogCounter counter, String owner, String message) {
        this.message = message;
        this.owner = owner;
        this.rule = counter.getRule();
    }

    public static String buildConfigErrorAlertMessage(AlertLogCounter counter, String owner, String message) {
        LogAlertMessage alertMessage = new LogAlertMessage(counter, owner, message);
        return buildConfigErrorAlertMessage(alertMessage);
    }

    @NotNull
    private static String buildConfigErrorAlertMessage(LogAlertMessage alertMessage) {
        StringBuilder builder = new StringBuilder();
        builder.append("负责人员：%s\n");
        builder.append("告警内容：%s");

        StringBuilder out = new StringBuilder();
        try (Formatter formatter = new Formatter(out)) {
            formatter.format(builder.toString(),
                    alertMessage.getOwner(),
                    alertMessage.getMessage());
        } catch (Exception e) {
            LOGGER.error("构建告警消息失败。", e);
        }

        return out.toString();
    }
}
